package com.handinfo.android.uicontrols.controls;

import java.util.concurrent.CopyOnWriteArrayList;

import android.graphics.Bitmap;
import android.graphics.PointF;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.utils.Tools;

/**
 * 网格控件
 * @author handinfo
 */
public class DWGrids extends DWControl
{

	private int m_num_w = 0;//横向格数
	private int m_num_h = 0;//纵向格数

	private int m_grid_w = 0;//每格宽度
	private int m_grid_h = 0;//每格高度

	private int m_show_w_num = 0;//显示横向格数
	private int m_show_h_num = 0;//显示纵向格数

//	private int m_num_open = 0;//开启的格数
	private int m_num_all = 0;//所有的格数

	private int m_interspace_w = 0;//横向空隙
	private int m_interspace_h = 0;//纵向空隙

	private int m_touch_index = 0;//触摸的索引
	private int m_touch_index_x = 0;//触摸的x索引
	private int m_touch_index_y = 0;//触摸的y索引
	
	private int m_drag_index = 0;//拖拽停放的下标
	private int m_drag_index_x = 0;
	private int m_drag_index_y = 0;

	private float m_offset_y = 0;

	public CopyOnWriteArrayList<DWGrid> m_grids = new CopyOnWriteArrayList<DWGrid>();//网格列表

	private Bitmap img_grid = null;

	private DWScrollBar m_scrollbar = null;

	/**
	 * 设置一个网格,默认全开启,样式为背包样式,空隙为0
	 * @param numW
	 * @param numH
	 */
	public DWGrids(Bitmap img, int num, int showW, int showH)
	{
		this(img, 0, 0, num, showW, showH);
	}

	/**
	 * 设置一个网格,默认全开启,样式为背包样式,空隙为0
	 * @param x
	 * @param y
	 * @param numW
	 * @param numH
	 */
	public DWGrids(Bitmap img, int x, int y, int num, int showW, int showH)
	{
		setPoint(x, y);
		setGridsAmount(img, num, showW, showH);
	}
	
	public DWGrids(int w, int h, int num, int showW, int showH) {
		this(0, 0, w, h, num, showW, showH);
	}
	
	public DWGrids(int x, int y, int w, int h, int num, int showW, int showH) {
		setPoint(x, y);
		setGridsAmount(w, h, num, showW, showH);
	}
	

	/**
	 * 设置背包总数
	 * @param numW
	 * @param numH
	 */
	private void setGridsAmount(int w, int h, int num, int showW, int showH)
	{
		m_grid_w = w;
		m_grid_h = h;

		m_num_w = showW;
		m_num_h = num / showW + (num % showW > 0 ? 1 : 0);

		m_interspace_w = 10;
		m_interspace_h = 10;

		m_num_all = num;

		m_show_w_num = showW;
		m_show_h_num = showH;

		setShowWideHigh(m_show_w_num * m_grid_w + m_show_w_num * m_interspace_w, m_show_h_num * m_grid_h + m_show_h_num
				* m_interspace_h);
		setActualWideHigh(m_num_w * m_grid_w + m_num_w * m_interspace_w, m_num_h * m_grid_h + m_num_h * m_interspace_h);

		if (m_actual_h > m_show_h) {
			m_scrollbar = new DWScrollBar(m_show_h);
			m_scrollbar.setNearAnchor(this, Tools.LEFT_TOP, Tools.RIGHT_TOP, 2, 0);
		}
		addOpenGrid(m_num_all);
	}
	
	/**
	 * 设置背包总数
	 * @param numW
	 * @param numH
	 */
	private void setGridsAmount(Bitmap img, int num, int showW, int showH)
	{
		if (img == null) {
			Tools.debugPrintln("Grids Image is null!");
		}
		img_grid = img;

		m_grid_w = img.getWidth();
		m_grid_h = img.getHeight();

		m_num_w = showW;
		m_num_h = num / showW + (num % showW > 0 ? 1 : 0);

		m_interspace_w = 10;
		m_interspace_h = 10;

		m_num_all = num;

		m_show_w_num = showW;
		m_show_h_num = showH;

		setShowWideHigh(m_show_w_num * m_grid_w + m_show_w_num * m_interspace_w, m_show_h_num * m_grid_h + m_show_h_num
				* m_interspace_h);
		setActualWideHigh(m_num_w * m_grid_w + m_num_w * m_interspace_w, m_num_h * m_grid_h + m_num_h * m_interspace_h);

		if (m_actual_h > m_show_h) {
			m_scrollbar = new DWScrollBar(m_show_h);
			m_scrollbar.setNearAnchor(this, Tools.LEFT_TOP, Tools.RIGHT_TOP, 2, 0);
		}
		addOpenGrid(m_num_all);
	}

	/**
	 * 设置显示高度
	 * @param showH
	 */
	public void setShowH(int showH)
	{
		m_show_h_num = showH;
		setShowWideHigh(m_show_w_num * m_grid_w + (m_show_w_num - 1) * m_interspace_w,
				m_show_h_num * m_grid_h + (m_show_h_num - 1) * m_interspace_h);
		if (m_actual_h > m_show_h) {
			m_scrollbar = new DWScrollBar(m_show_h);
			m_scrollbar.setNearAnchor(this, Tools.LEFT_TOP, Tools.RIGHT_TOP, 2, 0);
		}
	}

	/**
	 * 设置开启格数
	 * @param num
	 */
	public void addOpenGrid(int num)
	{
		if (num > 0) {
			DWGrid grid = null;
			for (int i = 0; i < num; i++) {
				if (img_grid == null) {
					grid = new DWGrid(getAllotX(i), getAllotY(i), m_grid_w, m_grid_h);
				} else {
					grid = new DWGrid(img_grid, getAllotX(i), getAllotY(i));
				}
				grid.setRelyControl(this);
				m_grids.add(grid);
			}
//			m_grids.get(0).setSelect(true);
		}
		else {
			Tools.debugPrintln("开启格数的数量错误 : " + num);
		}
	}

	/**
	 * 设置格子图片
	 * @param img
	 * @param index
	 */
	public void setGridImage(Bitmap img, int index)
	{
		setGridsImage(img, index, index);
	}

	/**
	 * 设置格子图片
	 * @param start 起始下标
	 * @param end 终点下标(包括当前end下标)
	 */
	public void setGridsImage(Bitmap img, int start, int end)
	{
		if (start <= end && start > -1 && end < m_grids.size()) {
			for (int i = start; i <= end; i++) {
				m_grids.get(i).setImage(img);
			}
		}
	}
	/**
	 * 设置是不是不能拖拽
	 * @param drag false为可以拖拽,true不能拖拽
	 */
	public void setDrag(boolean drag) {
		for (int i = 0; i < m_grids.size(); i++) {
			m_grids.get(i).setDarg(drag);
		}
	}
	/**
	 * 得到分配的x
	 * @param i
	 * @return
	 */
	private int getAllotX(int i)
	{
		int row = (i % m_num_w);
		return row * m_grid_w + row * m_interspace_w + m_interspace_w / 2;
	}

	/**
	 * 得到分配的y
	 * @param i
	 * @return
	 */
	private int getAllotY(int i)
	{
		int col = (i / m_num_w);
		return col * m_grid_h + col * m_interspace_h + m_interspace_h / 2;
	}

	/**
	 * 得到触摸的下标
	 * @return
	 */
	public int getTouchIndex()
	{
		return m_touch_index;
	}

	/**
	 * 得到触摸的x索引
	 * @return
	 */
	public int getTouchX()
	{
		return m_touch_index_x;
	}

	/**
	 * 得到触摸的y索引
	 * @return
	 */
	public int getTouchY()
	{
		return m_touch_index_y;
	}

	/**
	 * 得到拖拽的下标
	 * @return
	 */
	public int getDragIndex() {
		return m_drag_index;
	}
	
	/**
	 * 得到拖拽的x索引
	 * @return
	 */
	public int getDragX() {
		return m_drag_index_y;
	}
	/**
	 * 得到拖拽的y索引
	 * @return
	 */
	public int getDragY() {
		return m_drag_index_x;
	}
	/**
	 * 得到触摸的Grid
	 * @return
	 */
	public DWGrid getTouchGrid() {
		return m_grids.get(m_touch_index);
	}
	/**
	 * 得到拖放的Grid
	 * @return
	 */
	public DWGrid getDragGird() {
		return m_grids.get(m_drag_index);
	}
	
	/**
	 * 交换Grids内的数据
	 * @param dest
	 * @param src
	 */
	public void exchangeGrid(int dest, int src) {
		if (dest > -1 && dest < m_grids.size() && src  > -1 && src < m_grids.size()) {
			m_grids.get(dest).m_data = m_grids.get(src).m_data;
			m_grids.get(src).m_data = null;
		}
	}
	
	/**
	 * 设置空隙宽度
	 * @param w
	 * @param h
	 */
	public void setInterspace(int w, int h)
	{
		m_interspace_w = w;
		m_interspace_h = h;
		setShowWideHigh(m_show_w_num * m_grid_w + m_show_w_num * m_interspace_w, m_show_h_num * m_grid_h + m_show_h_num
				* m_interspace_h);
		setActualWideHigh(m_num_w * m_grid_w + m_num_w * m_interspace_w, m_num_h * m_grid_h + m_num_h * m_interspace_h);
		if (m_actual_h > m_show_h) {
			m_scrollbar = new DWScrollBar(m_show_h);
			m_scrollbar.setNearAnchor(this, Tools.LEFT_TOP, Tools.RIGHT_TOP, 2, 0);
		}
		for (int i = 0; i < m_grids.size(); i++) {
			m_grids.get(i).setPoint(getAllotX(i), getAllotY(i));
		}
	}

	public void refreshControl()
	{

	}

	public void render(DWGraphics g)
	{
		g.setClip(m_show_x, m_show_y, m_show_w, m_show_h);
		m_deviant_y = (int) m_offset_y;
		for (int i = 0; i < m_grids.size(); i++) {
			m_grids.get(i).renderLimits(g, m_show_x, m_show_y, m_show_w, m_show_h, (int)m_deviant_x, (int)m_deviant_y);
		}
		renderRect(g);
		if (m_scrollbar != null) {
			m_scrollbar.setDefaultNearAnchorUn();
			m_scrollbar.render(g);
		}
	}

	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		setSubClip(g, x, y, w, h, offsetx, offsety);
		m_deviant_y = (int) m_offset_y;
		for (int i = 0; i < m_grids.size(); i++) {
			m_grids.get(i).renderLimits(g, x + m_show_x, y + m_show_y, m_show_w, m_show_h, (int)m_deviant_x, (int)m_deviant_y);
		}
		renderLimitsRect(g, x, y, offsetx, offsety);
		if (m_scrollbar != null) {
			m_scrollbar.setDefaultNearAnchorUn();
			m_scrollbar.renderLimits(g, x, y, w, h, offsetx, offsety);
		}
	}

	public void logic()
	{
		
	}
	
	public boolean doDown(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			PointF newPoint = new PointF(point.x - m_show_x, point.y - m_show_y + m_offset_y);
			for (int i = 0; i < m_grids.size(); i++) {
				if (m_grids.get(i).doDown(newPoint)) {
					m_grids.get(m_touch_index).setSelect(false);
					m_touch_index = i;
					m_touch_index_x = i % m_num_w;
					m_touch_index_y = i / m_num_w;
					m_grids.get(m_touch_index).setSelect(true);
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean doLongPress(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			PointF newPoint = new PointF(point.x - m_show_x, point.y - m_show_y + m_offset_y);
			for (int i = 0; i < m_grids.size(); i++) {
				if (m_grids.get(i).doLongPress(newPoint)) {
					m_grids.get(m_touch_index).setSelect(false);
					m_touch_index = i;
					m_touch_index_x = i % m_num_w;
					m_touch_index_y = i / m_num_w;
					m_grids.get(m_touch_index).setSelect(true);
					DWControlsManager.getInstance().setDragControl(this);
					if (m_long_listener != null) {
						m_long_listener.OnLong(true);
					}
					setGuide(false);
					return true;
				}
			}
		}
		return false;
	}

	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y)) {
			for (int i = 0; i < m_grids.size(); i++) {
				if (m_grids.get(i).doClick(x - m_show_x, y - m_show_y + m_offset_y)) {
					m_grids.get(m_touch_index).setSelect(false);
					m_touch_index = i;
					m_touch_index_x = i % m_num_w;
					m_touch_index_y = i / m_num_w;
					m_grids.get(m_touch_index).setSelect(true);
					if (m_listener != null) {
						m_listener.OnClick();
					}
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean doDrag(PointF start, PointF end, float velocityX, float velocityY)
	{
		if (checkPointOnRect(end.x, end.y)) {
			PointF newStart = new PointF(start.x - m_show_x, start.y - m_show_y + m_offset_y);
			PointF newEnd = new PointF(end.x - m_show_x, end.y - m_show_y + m_offset_y);
			for (int i = 0; i < m_grids.size(); i++) {
				if (m_grids.get(i).doDrag(newStart, newEnd, velocityX, velocityY)) {
					m_drag_index = i;
					m_drag_index_x = i % m_num_w;
					m_drag_index_y = i / m_num_w;
					if (m_drag_listener != null) {
						m_drag_listener.OnDrag(DWControlsManager.getInstance().getDragControl());
					}
					return true;
				}
			}
		}
		return false;
	}

	public boolean doScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
		if (checkPointOnRect(start.x, start.y) && m_actual_h > m_show_h) {
			float b = m_actual_h / m_show_h;
			m_offset_y += (distanceY * 2) / b;
			if (m_offset_y < 0) {
				m_offset_y = 0;
			}
			else if (m_offset_y + m_show_h > m_actual_h) {
				m_offset_y = m_actual_h - m_show_h;
			}
			if (m_scrollbar != null) {
				m_scrollbar.setMark((int) (m_offset_y * 100 / (m_actual_h - m_show_h)));
			}
		}
		return false;
	}

}
